﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using ERP.Manager.BusinessServices;
using ERP.Manager.DataEntity;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class ECS_brandController : Controller
    {
        private readonly ECS_brandinfoService ecs_brandinfoservices;
        public ECS_brandController() { }
        public ECS_brandController(ECS_brandinfoService ecs_brandinfoservices)
        {
            this.ecs_brandinfoservices = ecs_brandinfoservices;
        }

        #region 显示商品品牌信息页面
        [CheckPersonnel]
        public ActionResult ShowECS_brand(int? page)
        {
            int pagecount = page ?? 1;
            List<ECS_brandinfo> list = ecs_brandinfoservices.all_getcs_brandinfo_page(pagecount);
            ViewData["ecs_brandinfo"] = list;
            ViewData["ecs_brandinfo_count"] = ecs_brandinfoservices.all_getecs_brandinfo_count();
            return View();
        }
        #endregion

        #region 添加商品品牌信息页面
        public ActionResult AddECS_brand()
        {
            return View();
        }
        #endregion

        #region Ajax添加商品品牌信息
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxAddECS_brand(string brand_name, string brand_logo, string brand_desc, string site_url, string sort_order, string is_show)
        {
            try
            {
                ECS_brandinfo brand = new ECS_brandinfo();
                brand.brand_name = brand_name;
                brand.brand_logo = brand_logo;
                brand.brand_desc = brand_desc;
                brand.site_url = site_url == null ? "http://" : site_url;
                brand.sort_order = int.Parse(sort_order);
                brand.is_show = int.Parse(is_show);
                int rowcount = ecs_brandinfoservices.add_ecs_brandinfo(brand);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 修改商品品牌页面
        public ActionResult UpdateECS_brand(string brand_id)
        {
            ECS_brandinfo brand = ecs_brandinfoservices.detail_ecs_brandinfo(int.Parse(brand_id));
            ViewData["ECS_brandinfo"] = brand;
            return View();
        }
        #endregion

        #region 修改商品品牌页面
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxUpdateECS_brand(string brand_id, string brand_name, string brand_logo, string brand_desc, string site_url, string sort_order, string is_show)
        {
            try
            {
                ECS_brandinfo brand = new ECS_brandinfo();
                brand.brand_id = int.Parse(brand_id);
                brand.brand_name = brand_name;
                brand.brand_desc = brand_desc;
                brand.site_url = site_url == null ? "http://" : site_url;
                brand.sort_order = sort_order == "" ? 50 : int.Parse(sort_order);
                brand.is_show = int.Parse(is_show);
                ECS_brandinfo and = ecs_brandinfoservices.detail_ecs_brandinfo(brand.brand_id);
                brand.brand_logo = brand_logo == "" ? and.brand_logo : "";
                int rowcount = ecs_brandinfoservices.update_ecs_brandinfo(brand);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return ex.Message;
            }
        }
        #endregion

        #region 删除商品品牌信息的方法
        public string AjaxDeleteECS_brandinfo_brand_id(string brand_id)
        {
            int id = int.Parse(brand_id.ToString());
            try
            {
                int rowcount = ecs_brandinfoservices.delete_ecs_brandinfo(id);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion

        #region 修改商品的显示状态
        public string Ajaxupdate_ecs_brand_is_show(int brand_id, int is_show)
        {
            try
            {
                int rowcount = ecs_brandinfoservices.update_ecs_brand_is_show(brand_id, is_show);
                if (rowcount > 0)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        #endregion
    }
}
